import { Component, OnInit, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { RecordVal } from '@services/data-types/member.type';
import { RecordType } from '@services/member.service';
import { Singer, Song } from '@services/data-types/common.type';
import { BatchActionsService } from '@store/batch-actions.service';
import { ModalTypes, ShareInfo } from '@store/reducers/member.reducer';

@Component({
  selector: 'app-records',
  templateUrl: './records.component.html',
  styleUrls: ['./records.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RecordsComponent implements OnInit {
  @Input() records: RecordVal[];
  @Input() recordType = RecordType.weekData;
  @Input() listenSongs = 0;
  @Input() currentIndex = -1;
  @Output() onAddSong = new EventEmitter<[Song, boolean]>();
  @Output() onChangeType = new EventEmitter<number>();
  constructor(private batchActionsService: BatchActionsService) {}

  ngOnInit() {
  }
  // 收藏歌曲
  onLikeSong(songs: Song) {
    const ids = songs.id.toString();
    this.batchActionsService.likeSong(ids);
  }
  // 分享歌曲
  onShareSong(song: Song, type = 'song') {
    const txt = this.makeTxt('歌曲', song.name, song.ar);
    const info: ShareInfo = {id: song.id.toString(), type, txt};
    this.batchActionsService.setShareInfo(info);
    this.batchActionsService.controlModalVisible(true, ModalTypes.Share);
  }
  private makeTxt(type: string, name: string, makeBy: string | Singer[]): string {
    let needText = '';
    if (Array.isArray(makeBy)) {
      needText = makeBy.map(item => item.name).join('/');
    } else {
      needText = makeBy;
    }
    return `${type}: ${name} -- ${needText}`;
  }
}
